# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# File       : alarm_view.py
# Time       ：2023/11/2 10:33
# Author     ：YangYong
# version    ：python 3.10
# Description：日志视图
"""
from flask_restx import fields

from apis.notice import notice_ns
from apis.notice.model.logger_info import LoggerInfo
from apis.notice.schema.logger_schema import LoggerInfoSchema
from public.api_utils.api_decorator import handle_api_error
from public.base_response import MarshalModel
from public.base_view import BaseView, BaseDetailsView

logger_model = {
    'api_path': fields.String(description='api路径'),
    'method': fields.String(description='请求方法'),
    'state': fields.String(description='日志等级'),
    'visitor': fields.String(description='访问人'),
    'message': fields.String(description='日志内容'),
}
logger_marshal = MarshalModel('logger', notice_ns)


class LoggerDetailsView(BaseDetailsView):
    url = '/logger/detail'

    def __init__(self, *args, **kwargs):
        super(LoggerDetailsView, self).__init__(*args, **kwargs)

    @handle_api_error()
    @notice_ns.doc(description="获取日志详情")
    @notice_ns.expect(logger_marshal.detail_request())
    @notice_ns.marshal_with(logger_marshal.detail_response(logger_model))
    def get(self):
        """获取日志详情"""
        return super(LoggerDetailsView, self).get()


class LoggerView(BaseView):
    url = '/logger'

    def __init__(self, *args, **kwargs):
        super(LoggerView, self).__init__(*args, **kwargs)
        self.table_orm = LoggerInfo
        self.table_schema = LoggerInfoSchema

    @notice_ns.doc(description="获取日志列表")
    @notice_ns.expect(logger_marshal.list_request(logger_model))
    @notice_ns.marshal_with(logger_marshal.list_response(logger_model))
    def get(self):
        """获取日志列表"""
        return super(LoggerView, self).get()

    @notice_ns.doc(description="删除日志")
    @notice_ns.expect(logger_marshal.delete_request())
    @notice_ns.marshal_with(logger_marshal.delete_response())
    def delete(self):
        """删除日志"""
        return super(LoggerView, self).delete()

    @handle_api_error()
    @notice_ns.hide
    def put(self):
        """修改日志"""
        return super(LoggerView, self).put()

    @handle_api_error()
    @notice_ns.hide
    def post(self):
        return super(LoggerView, self).post()
